﻿using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using SwaggerDoc.Api.Model;

namespace SwaggerDoc.Api.Controllers
{
    /// <summary>
    /// 主控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiVersionNeutral]
    //[Authorize]
    public class HomeController : ControllerBase
    {
        public readonly SqlSugarScope _sqlSugarClient;
        public HomeController(SqlSugarScope sqlSugarClient)
        {
            _sqlSugarClient= sqlSugarClient;
        }

        /// <summary>
        /// Health
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Health()
        {
            return "ok";
        }

        /// <summary>
        /// 不受版本控制
        /// </summary>
        /// <returns></returns>
        [HttpGet("VersionNeutral")]
        [XCodeSamples(lang: "C#", source: @"
var client = new RestClient(""https://example.com/webapi/a2/users/{user_id}"");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader(""webapi_token"", ""your secret key"");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);")

 , XCodeSamples(lang: "Java", source: @"
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
                     .url(""https://example.com/webapi/a2/users/{user_id}"")
                     .method(""GET"", null)
                     .addHeader(""webapi_token"", ""your secret key"")
                     .build();
Response response = client.newCall(request).execute();")

 , XCodeSamples(lang: "PHP", source: @"
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => ""https://example.com/webapi/a2/users/%7Buser_id%7D"",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => """",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => ""GET"",
CURLOPT_HTTPHEADER => array(
""webapi_token: your secret key""
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;")

 , XCodeSamples(lang: "Python", source: @"
import http.client
import mimetypes

conn = http.client.HTTPSConnection(""example.com"", 443)
payload = ''
headers = {
'webapi_token': 'your secret key'
}
conn.request(""GET"", ""/webapi/a2/users/{user_id}"", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode(""utf-8""))")
]
        public IActionResult VersionNeutral()
        {
            return Ok();
        }

        /// <summary>
        /// 枚举Enum
        /// </summary>
        /// <returns></returns>
        [HttpPut("Enum")]
        public IActionResult Enum([FromBody] SwaggerModel swaggerModel)
        {
            return Ok();
        }

        /// <summary>
        /// 空
        /// </summary>
        /// <returns></returns>
        [HttpGet("Null")]
        public IActionResult Null()
        {
            var queryable=_sqlSugarClient.Queryable<WeatherForecast>().ToList();

            var sqlQueryable = _sqlSugarClient.SqlQueryable<WeatherForecast>("SELECT * FROM WeatherForecast").ToList();

            var fromdb = _sqlSugarClient.Ado.SqlQuerySingle<WeatherForecast>("SELECT * FROM WeatherForecast WHERE Id=1");

            var dto = _sqlSugarClient.Ado.SqlQuery<WeatherForecastDTO>("SELECT * FROM WeatherForecast");

            return Ok( new {
                fromdb = fromdb,
                dto = dto
            });
        }

        /// <summary>
        /// PostTest
        /// </summary>
        /// <returns></returns>
        [HttpPost("PostTest")]
        public IActionResult PostTest([FromBody] PostTest postTest)
        {
            //var recieve = postTest.Status;
            return Ok(postTest);
        }

        [HttpPost("Validator")]
        public ActionResult Validator([FromBody] UserRequest input)
        {
            return Ok("Validator");
        }
    }
}
